#This is to get:
#Figure 3

rm(list = ls())
library("rgdal")
library("ggplot2")
library("raster")
library("lemon")
library("ggsn")
library("raster")

setwd("C:/Users/bogdanp/Dropbox/Legacies_Central_Europe")
setwd("/Users/bgpopescu/Dropbox/Legacies_Central_Europe")

#Step1: Read csv
data<-read.csv(file = './data/print_uni_central_europe.csv')

#Step2: Reading DEM
euro_raster <- raster("./data/rasters/euro_raster.tif")

#Step3: Convert rasters TO dataframes for plotting with ggplot
hdf <- rasterToPoints(euro_raster)
hdf <- data.frame(hdf)
colnames(hdf) <- c("X","Y","Hill")

#Step4: Create vectors for colour breaks
b.hs <- seq(min(hdf$Hill),max(hdf$Hill),length.out=100)

#Step5: Reading shapefile for countries
all_coun <- readOGR(dsn="./data/shape_files.gdb",
                    layer="all_country_boundaries", use_iconv = TRUE, encoding = "UTF-8")
all_coun@data$id <-rownames(all_coun@data)
all_coun_fort<-fortify(all_coun)

#Step6; Creating categories for map
data$type
printshops<-subset(data, type == "printshop")
data$foundation_year
unique(printshops$foundation_year)
col = c("before 1500"="black",
        "1500-1599"="red",
        "1600-1699"="yellow")


order_col<-c("before 1500", "1500-1599", "1600-1699")

#Step7: Making the map
map<-ggplot() + 
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  # use the "alpha hack"
  scale_alpha(name = "Altitude in m", guide="none")  + 
  geom_path(data = all_coun_fort, colour = "black", aes(x=long, y=lat, group = group), size = 0.4)+
  geom_point(data=printshops, 
             size = 3,
             aes(x =lon, y= lat, 
                 color=factor(foundation_year)), alpha=0.5)+
  scale_colour_manual(values = col,
                      name = "Time",
                      breaks = order_col)+
  coord_cartesian(ylim=c(min(data$lat), max(data$lat)), xlim=c(min(data$lon), max(data$lon)))+
    theme_bw()+
    labs(x = "Longitude (X)", y="Latitude (Y)")+
    #ggtitle("Printing Press in Europe")+
    theme(plot.title = element_text(hjust = 0.5))+
    theme()+
    theme(legend.position = c(1, 0),
          #Legend.position values should be between 0 and 1. c(0,0) corresponds to the "bottom left"
          #and c(1,1) corresponds to the "top right" position.
          legend.box.background = element_rect(fill='white'),
          legend.background = element_blank(),
          legend.text=element_text(size=12))+
           scalebar(x.min = 27, x.max = 31,
           #Above you mention how long the scalebar should be
           y.min = 55, y.max = 55.1,
           #Above you mention how thick the scalebar should be
           dist = 200, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "bottomright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =3,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=3)

map2<-reposition_legend(map, 'bottom left')

ggsave(map2,file="./Paper/graphs/figure3.jpg", height=19.42, width=20.2, units = "cm", dpi=250)

